package com.zdy.im.user.facade;

import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.zdy.im.user.dto.response.ImRoomUserShipResponseDto;
import com.zdy.im.user.dto.response.UserInfoResponseDto;
import com.zdy.im.user.service.ImRoomUserShipService;
import com.zdy.im.user.service.UserInfoService;
import com.zdy.im.user.vo.request.ImUserFriendQueryRequestVo;
import com.zdy.im.user.vo.request.UserRoomFriendQueryRequestVo;
import com.zdy.im.user.vo.response.ImUserFriendShipShowResponseVo;
import com.zdy.im.user.dto.response.ImUserFriendShipResponseDto;
import com.zdy.im.user.service.ImUserFriendShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import org.springframework.util.CollectionUtils;

import java.util.*;


/**   
 * @Title: ImUserFriendShipFacade.java
 * @Author 作者 : zdy
 * @CreateDate 创建时间: 2021-02-30
 * @version V1.0  
 *  <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 * @Description: TODO(说明) 对象缓存统一模板实现类;  
 */
 
@Component
public class ImUserFriendShipFacade {

	@Autowired
	private ImUserFriendShipService imUserFriendShipService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private ImRoomUserShipService imRoomUserShipService;

	public ResponseResultList<ImUserFriendShipShowResponseVo> selectListByUserId(ImUserFriendQueryRequestVo imUserFriendQueryRequestVo){
		List<ImUserFriendShipResponseDto> resultList = imUserFriendShipService.selectListByUserId(imUserFriendQueryRequestVo.getUserId(),imUserFriendQueryRequestVo.getFriendRemarks());
		if(null == resultList || resultList.isEmpty() ){
			return ResponseResultList.build();
		}
		Set<Long> userIds = new HashSet<>();
		resultList.forEach(e ->{
			if(e.getFriendId() == e.getUserId()) {
				return ;
			}
			userIds.add(e.getFriendId());
		});
		Map<Long, UserInfoResponseDto> userMap = userInfoService.getUserInfoByIds( userIds);

		List<ImUserFriendShipShowResponseVo> listVo = new ArrayList<>();
		resultList.forEach(e -> {
			if(e.getFriendId() == e.getUserId()) {
				return ;
			}
			ImUserFriendShipShowResponseVo vo = ImUserFriendShipShowResponseVo.build().clone(e);
			if(!CollectionUtils.isEmpty(userMap) && userMap.get(e.getFriendId()) != null){
				vo.setFriendHeadImg(userMap.get(e.getFriendId()).getHeadImg());
				vo.setUserName(userMap.get(e.getFriendId()).getUserName());//用户昵称
			}

			listVo.add(vo);
		});
		return ResponseResultList.build().toList(listVo).toTotal(listVo.size());
	}

	/**
	 * 群加好友--可添加好友列表信息
	 * @param imUserFriendQueryRequestVo
	 * @return
	 */
	public ResponseResultList<ImUserFriendShipShowResponseVo> selectRoomFridenList(UserRoomFriendQueryRequestVo imUserFriendQueryRequestVo){
		List<ImUserFriendShipResponseDto> resultList = imUserFriendShipService.selectListByUserId(imUserFriendQueryRequestVo.getUserId(),imUserFriendQueryRequestVo.getFriendRemarks());
		if(null == resultList || resultList.isEmpty() ){
			return ResponseResultList.build();
		}
		List<ImUserFriendShipResponseDto> results = new ArrayList<>();
		//查询群，已添加好友
		Map<Long, ImRoomUserShipResponseDto> roomUserMap = imRoomUserShipService.selectMapByRoomId(imUserFriendQueryRequestVo.getRoomId());
		if(!CollectionUtils.isEmpty(roomUserMap)){
			resultList.forEach(u->{
				//剔除已添加过的
				if(roomUserMap.get(u.getFriendId()) == null){
					results.add(u);
				}
			});
		}
		if(null == results || results.isEmpty() ){
			return ResponseResultList.build();
		}
		Set<Long> userIds = new HashSet<>();
		results.forEach(e ->{
			userIds.add(e.getFriendId());
		});

		//查询用户信息，头像信息
		Map<Long, UserInfoResponseDto> userMap = userInfoService.getUserInfoByIds( userIds);

		List<ImUserFriendShipShowResponseVo> listVo = new ArrayList<>();
		results.forEach(e -> {
			ImUserFriendShipShowResponseVo vo = ImUserFriendShipShowResponseVo.build().clone(e);
			if(!CollectionUtils.isEmpty(userMap) && userMap.get(e.getFriendId()) != null){
				vo.setFriendHeadImg(userMap.get(e.getFriendId()).getHeadImg());
				vo.setUserName(userMap.get(e.getFriendId()).getUserName());//用户昵称
			}

			listVo.add(vo);
		});
		return ResponseResultList.build().toList(listVo).toTotal(listVo.size());
	}
}
